new Vue({
    el: '#app',
    data() {
        return {
            form: {
                page: 1,			//页数
                limit: 10,			//每页数据条数
                Page_Group_SerialNumber: '',
                Page_Type_SerialNumber: '',//页面类型
            },
            total: 0,
            tableData: [],		//表格数据
            Data: [],
            Page_Group_SerialNumber: '0',
            PageGroup: []//方案
        }
    },
    mounted() {
        this.getData();
        console.log(this.NewData);
    },
    watch: {
        Page_Group_SerialNumber(val) {
            this.form.Page_Group_SerialNumber = val
            this.getList(this.form);
            this.getTree(val);
        }
    },
    computed: {
        treeData() {
            let items = this.Data
            // 创建一个以id为键，值为对应对象的映射  
            const map = {};
            items.forEach(item => {
                map[item.Page_Type_SerialNumber] = { ...item, children: [] }
            });
            // 遍历所有项，将每个项添加到其父项的children数组中  
            items.forEach(item => {
                const parentId = item.Parent_Page_SerialNumber;
                if (parentId && map[parentId]) {
                    map[parentId].children.push(map[item.Page_Type_SerialNumber]);
                }

            });
            // 提取根节点（即parentId为null或未定义的项）  
            const rootNodes = items.filter(item => !item.Parent_Page_SerialNumber || map[item.Parent_Page_SerialNumber] === undefined).map(item => map[item.Page_Type_SerialNumber]);
            // 如果只需要根节点的数组，返回它  
            return rootNodes;
        }
    },
    methods: {
        handleNodeClick(node) {//点击节点
            this.form.page = 1;
            this.form.Page_Type_SerialNumber = node.Page_Type_SerialNumber
            this.getList(this.form)
        },
        onSerach() {				//查询方法
            this.form.page = 1;
            this.getList(this.form)
        },
        handleSizeChange(val) {
            this.form.page = 1;
            this.form.limit = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.page = val;
            this.getList(this.form)
        },
        getData() {//查询方案
            let _this = this;
            $.ajax({
                type: 'POST',
                url: `${DomainName}/getInquiryAction?token=${$.cookie('token')}`,
                data: {
                    Data: 'page_group'
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                success: function (Result) {
                    if (Result.length > 0) {
                        _this.Page_Group_SerialNumber = Result[0].Page_Group_SerialNumber
                        _this.PageGroup = Result
                    }
                },
                dataType: "json"
            });
        },
        getTree(Page_Group_SerialNumber) {
            let _this = this
            $.ajax({
                type: 'POST',
                url: `${DomainName}/getPageTreeAction?token=${$.cookie('token')}`,
                data: {
                    Page_Group_SerialNumber
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                success: function (Result) {
                    if (Result.Status == 'success') {
                        _this.Data = Result.Data;
                    }
                },
                dataType: "json"
            });
        },
        getList(data) {     //换页方法
            let _this = this
            $.ajax({
                type: 'POST',
                url: `${DomainName}/getPageAction?token=${$.cookie('token')}`,
                data,
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                success: function (Result) {
                    if (Result.Status == 'success') {
                        _this.tableData = Result.tableData
                        _this.total = parseInt(Result.limit)
                    }
                },
                dataType: "json"
            });
        },
        Config(Page_SerialNumber) {
            sessionStorage.setItem("Page_SerialNumber", Page_SerialNumber)
            window.location.href = './Config.html?is=config&Page_SerialNumber=' + Page_SerialNumber
        },
        Add() {
            sessionStorage.setItem("Page_Group_SerialNumber", this.Page_Group_SerialNumber)
            window.location.href = './Add.html?Page_Group_SerialNumber=' + this.Page_Group_SerialNumber
        },
        AddGroup(){
            window.location.href = './AddGroup.html'
        },
        Edit(Page_SerialNumber) {
            sessionStorage.setItem("Page_Group_SerialNumber", this.Page_Group_SerialNumber)
            sessionStorage.setItem("Page_SerialNumber", Page_SerialNumber)
            window.location.href = './Edit.html?Page_SerialNumber=' + Page_SerialNumber
        },
        Delete(Page_SerialNumber) {
            const _this = this
            this.$confirm('警告！此操作将删除此页面, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                $.ajax({
                    url: DomainName + '/DeleteAction?token=' + $.cookie('token'),
                    type: 'POST',
                    data: {
                        TableName: 'page_info',
                        Primary_Key: Page_SerialNumber,
                        Primary_Key_Name: 'Page_SerialNumber',
                        Operation_Name: '删除页面'
                    },
                    success: function (res) {
                        if (res.Status == 'success') {
                            _this.getList(_this.form);
                            _this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                        }
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'warning',
                    message: '已取消操作'
                });
            });
        },
    },
})